package com.itheima.controller;

import com.itheima.aop.annotation.LogOperater;
import com.itheima.aop.annotation.MyLog;
import com.itheima.pojo.Dept;
import com.itheima.pojo.Result;
import com.itheima.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author：cxl
 * @Package：com.itheima.controller
 * @Project：web-project
 * @name：DeptController
 * @Date：2025-09-25 9:41 am
 * @Filename：DeptController
 */
@Slf4j
@RestController

//请求分类
@RequestMapping("/depts")//添加之后相当于类上的加方法上的
public class DeptController {
    @Autowired
    private DeptService deptService;

    // @RequestMapping(method = {RequestMethod.GET,RequestMethod.POST}, value = "/depts")
    // @GetMapping("/depts")
    @LogOperater
    @GetMapping
    public Result list() {
        List<Dept> list = deptService.findAll();
        log.debug("查询部门列表");
        return Result.success(list);
    }

    //@RequestParam:
    // value 给参数取别名
    //defaultValue 默认值
    //required 是否必须传递参数
    @LogOperater
    @DeleteMapping
    public Result delete(@RequestParam Integer id) {
        log.info("根据id删除部门id" + id);
        deptService.deleteById(id);
        return Result.success();
    }

    //新增部门
    /*@RequestBody接收前端传过来的json数据{}
    实体属性要和请求中的key一致
    适合POST和PUT请求
    * */
    @LogOperater
    @PostMapping
    public Result add(@RequestBody Dept dept) {
        log.info("添加部门" + dept);
        deptService.add(dept);
        return Result.success();
    }

    //修改 部门
    @LogOperater
    @PutMapping
    public Result update(@RequestBody Dept dept) {
        log.info("修改部门" + dept);
        Dept dept1 = deptService.update(dept);
        return Result.success(dept1);
    }

    //根据id查询部门
    /*
     * {id}在路径中的参数，必须使用@PathVariable
     * 如果参数名和形参一样可以不用加value
     * */
    @LogOperater
    @GetMapping("/{id}")
    // public Result getById(@PathVariable(value = "id") Integer id) {
    public Result getById(@PathVariable Integer id) {
        log.info("根据id查询部门id=" + id);
        //alt +enter
        Dept dept = deptService.getById(id);
        return Result.success(dept);
    }

}
